package com.wyp168.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.wyp168.common.valid.AddGroup;
import com.wyp168.common.valid.DeleteGroup;
import com.wyp168.common.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.wyp168.gulimall.product.entity.BrandEntity;
import com.wyp168.gulimall.product.service.BrandService;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.R;


/**
 * 品牌
 *
 * @author wyp168
 * @email 2721255240@qq.com
 * @date 2022-10-22 12:09:10
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/){

//        if (result.hasErrors()) {   //如果校验到错误
//            //获取校验的错误结果
//            Map<String, String> errorMessageMap = new HashMap<>();
//            result.getFieldErrors().forEach(item -> {
//                //获取到错误字段名
//                String field = item.getField();
//                //获取到错误字段提示信息
//                String defaultMessage = item.getDefaultMessage();
//                errorMessageMap.put(field, defaultMessage);
//            });
//            return R.error(400, "提交的数据不合法").put("errorValids", errorMessageMap);
//        }
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    @PostMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@RequestBody BrandEntity brand){
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@Validated({DeleteGroup.class}) @RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
